//
//  AlignCollectionViewFlowLayout.m
//  ObjectiveCTools
//
//  Created by aidong on 2021/10/28.
//  Copyright © 2021 姬友大人. All rights reserved.
//

#import "AlignCollectionViewFlowLayout.h"

@interface AlignCollectionViewFlowLayout(){
    //在居中对齐的时候需要知道这行所有cell的宽度总和
    CGFloat _sumCellWidth ;
}
@end

@implementation AlignCollectionViewFlowLayout
-(instancetype)init{
    return [self initWithType:AlignWithCenter betweenOfCell:5.0];
}
-(void)setBetweenOfCell:(CGFloat)betweenOfCell{
    _betweenOfCell = betweenOfCell;
    self.minimumInteritemSpacing = betweenOfCell;
}
-(instancetype)initWthType:(AlignType)cellType{
    return [self initWithType:cellType betweenOfCell:5.0];
}
-(instancetype)initWithType:(AlignType)cellType betweenOfCell:(CGFloat)betweenOfCell{
    self = [super init];
    if (self){
        self.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.minimumLineSpacing = 5;
        self.minimumInteritemSpacing = betweenOfCell;
        self.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        _betweenOfCell = betweenOfCell;
        _cellType = cellType;
    }
    return self;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    //用来获取当前视图里面的 头, 尾, 单元格信息, 能获取到其中的 NSIndexPath 和 frme 信息
    NSArray * layoutAttributes_t = [super layoutAttributesForElementsInRect:rect];
    //新建一个数组,把上方的数组深度复制一份
    NSArray * layoutAttributes = [[NSArray alloc]initWithArray:layoutAttributes_t copyItems:YES];
    //用来临时存放一行的Cell数组
    NSMutableArray * layoutAttributesTemp = [[NSMutableArray alloc]init];
    //开始循环
    for (NSUInteger index = 0; index < layoutAttributes.count ; index++) {
        
        // 当前cell的位置信息
        UICollectionViewLayoutAttributes *currentAttr = layoutAttributes[index];
        // 上一个cell 的位置信
        UICollectionViewLayoutAttributes *previousAttr = index == 0 ? nil : layoutAttributes[index-1];
        //下一个cell 位置信息 如果是正好等于数组个数,那么就没有下一个,否则就能有下一个
        UICollectionViewLayoutAttributes *nextAttr = index + 1 == layoutAttributes.count ? nil : layoutAttributes[index+1];
        
        //加入临时数组
        [layoutAttributesTemp addObject:currentAttr];
        _sumCellWidth += currentAttr.frame.size.width;
        
        //前面一个格子的最大 Y 值, 有格子,就取到 y, 没有格子,就是0
        CGFloat previousY = previousAttr == nil ? 0 : CGRectGetMaxY(previousAttr.frame);
        //当前格子的Y, 跟随前面一个格子来获得
        CGFloat currentY = CGRectGetMaxY(currentAttr.frame);
        //下一个格子的Y
        CGFloat nextY = nextAttr == nil ? 0 : CGRectGetMaxY(nextAttr.frame);
        
        //如果当前cell是单独一行
        if (currentY != previousY && currentY != nextY){ //当前的格子,和上一个不在一行,和下一个也不在一行,
            if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
                [layoutAttributesTemp removeAllObjects];
                _sumCellWidth = 0.0;
            } else if ([currentAttr.representedElementKind isEqualToString:UICollectionElementKindSectionFooter]){
                [layoutAttributesTemp removeAllObjects];
                _sumCellWidth = 0.0;
            } else {
                [self setCellFrameWith:layoutAttributesTemp];
            }
        }
        //如果下一个cell在本行，这开始调整Frame位置
        else if( currentY != nextY) {
            [self setCellFrameWith:layoutAttributesTemp];
        } else {
            //当前一个 和 下一个 的 Y 值相同, 啥也不用做
        }
    }
    return layoutAttributes;
}

/*
 对于每一行的cell来说, 系统在布局的时候,假如这一行放置了3个cell,那么就说明,这一行剩下的空间已经不能放置第4个cell了, 如果能放置第4个cell,那么系统就帮你放进去了.
 所以,在你调整的时候,你所要做的就是挪动这一行的3个cell的位置,靠左,靠右,或者居中,而不必去考虑这一行挪动之后,剩下的空间是否还能放下第4个cell. 因为,如果能放,系统就帮你放下去了
 */


//调整属于同一行的cell的位置frame
-(void)setCellFrameWith:(NSMutableArray*)layoutAttributes{
    CGFloat nowWidth = 0.0;
    switch (_cellType) {
        case AlignWithLeft:
            nowWidth = self.sectionInset.left;
            for (UICollectionViewLayoutAttributes * attributes in layoutAttributes) {
                CGRect nowFrame = attributes.frame;
                nowFrame.origin.x = nowWidth;
                attributes.frame = nowFrame;
                nowWidth += nowFrame.size.width + self.betweenOfCell;
            }
            _sumCellWidth = 0.0;
            [layoutAttributes removeAllObjects];
            break;
            
        case AlignWithCenter:
            nowWidth = (self.collectionView.frame.size.width - _sumCellWidth - ((layoutAttributes.count - 1) * _betweenOfCell)) / 2;
            for (UICollectionViewLayoutAttributes * attributes in layoutAttributes) {
                CGRect nowFrame = attributes.frame;
                nowFrame.origin.x = nowWidth;
                attributes.frame = nowFrame;
                nowWidth += nowFrame.size.width + self.betweenOfCell;
            }
            _sumCellWidth = 0.0;
            [layoutAttributes removeAllObjects];
            break;
            
        case AlignWithRight:
            nowWidth = self.collectionView.frame.size.width - self.sectionInset.right;
            for (NSInteger index = layoutAttributes.count - 1 ; index >= 0 ; index-- ) {
                UICollectionViewLayoutAttributes * attributes = layoutAttributes[index];
                CGRect nowFrame = attributes.frame;
                nowFrame.origin.x = nowWidth - nowFrame.size.width;
                attributes.frame = nowFrame;
                nowWidth = nowWidth - nowFrame.size.width - _betweenOfCell;
            }
            _sumCellWidth = 0.0;
            [layoutAttributes removeAllObjects];
            break;
    }
}

@end
